/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2008-2014 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTH_REX_MESH_EDITOR
#define OSGEARTH_REX_MESH_EDITOR 1

#include "Common"
#include <osgEarth/TileKey>
#include <osgEarth/TerrainConstraintLayer>
#include <osgEarth/Feature>
#include <osgEarth/Threading>
#include <osg/Geometry>

namespace osgEarth {
    class Map;
}

namespace osgEarth { namespace REX
{
    using namespace osgEarth;

    class SharedGeometry;
    
    /**
     * Creates a tile mesh by cutting in feature-based constraints.
     */
    class MeshEditor
    {
    public:
        //! Construct a mesh editor for the given tile key
        MeshEditor(
            const TileKey& key,
            unsigned tileSize,
            const Map* map,
            ProgressCallback* progress);

        //! A mesh "edit" data operation (set of features to incorporate)
        struct Edit
        {
            FeatureList _features;
            osg::ref_ptr<TerrainConstraintLayer> _layer;
        };

        //! Whether any edits were found for the given tile key
        bool hasEdits() const
        {
            return !_edits.empty();
        }

        bool tileEmpty() const
        {
            return _tileEmpty;
        }

        //! Generate a mesh and populate the given SharedGeometry,
        //! optionally building skirts
        bool createTileMesh(
            SharedGeometry* geom,
            unsigned tileSize,
            double skirtHeightRatio,
            GLenum mode,
            Cancelable* progress);

    protected:
        std::vector<Edit> _edits;
        const TileKey _key;
        unsigned _tileSize;
        bool _tileEmpty;
    };
} }

#endif // OSGEARTH_REX_MESH_EDITOR
